﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Threading;
using BLL.TcpUdp;
using BLL.Dataprogram;
using System.Reflection;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Data;
using Framework.Kit;
using BLL.Action;
using BLL.Model;
using BLL.Service;

namespace Server
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /*************************页面局部变量声明************************/
        private Hashtable m_ChannelSock = new Hashtable();
        //服务器时钟
        private DispatcherTimer timer = new DispatcherTimer();
        DateTime currentTime = new System.DateTime();
        //任务管理时钟
        private DispatcherTimer eventTimer = new DispatcherTimer();

        public MainWindow()
        {
            //构建拍卖会选择界面            
            InitializeComponent();
            initForm();
            this.Show();

            //处理客户端连接数已满事件
            App.svr.ServerFull += new NetEvent(this.ServerFull);
            //处理新客户端连接事件
            App.svr.ClientConn += new NetEvent(this.ClientConn);
            //处理客户端关闭事件
            App.svr.ClientClose += new NetEvent(this.ClientClose);
            //处理接收到数据事件
            App.svr.RecvData += new NetEvent(this.RecvData);
        }
        // 初始化事件
        private void initForm()
        {
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            timer.Start();
            //
            eventTimer.Tick += new EventHandler(eventTimer_Tick);
            eventTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            eventTimer.Start();

            /***********初始化服务器资源***********/
            terminalService terService = new terminalService();
            terService.SetTerminalMap();
            App.startServer();

            /***************窗体控件位置重绘***************/
            this.Width = SystemParameters.WorkArea.Width;
            this.Height = SystemParameters.WorkArea.Height;
            this.Top = this.Left = 0;
            this.MaxHeight = this.MinHeight = this.Height;
            this.MaxWidth = this.MinWidth = this.Width;
            FMain.Height = SystemParameters.WorkArea.Height - 190;
            userLV.Height = FMain.Height - 20;
            targetTC.Height = userLV.Height - 200;
            targetSP.Width = this.Width - 250;

            /***************初始化拍卖会内容***************/
            auction auction = new auction();
            auctionModel auctionM = new auctionModel();

            auctionM = auction.getAuction(App.currentAuctionNo);
            auctionNameLbl.Content = auctionM.Auction_NAME;
            auctionAddLbl.Content = auctionM.Auction_ADD;
            auctionTimeLbl.Content = auctionM.Auction_DATE.ToShortDateString();

            /***************绑定标的列表***************/
            targetDG.ItemsSource = Target.GetSampleCustomerList();
        }


        //启动标的操作
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            Target c = b.Tag as Target;
            string Text = "你选择" + c.Name + "的Index是：" + c.Index + ",它位于 DataGrid 的第 " + c.Index + " 行";
            //MessageBox.Show(targetDG.Columns[5].GetCellContent(c).FindName("Button").ToString());
            MessageBox.Show(Text);

            //argetDG.Columns[5].GetCellContent(e).Visibility=Visibility.Hidden;
            //btn.IsEnabled = false;
        }

        //每隔一秒，时钟跳到一次
        void timer_Tick(object sender, EventArgs e)
        {
            currentTime = System.DateTime.Now;
            hour_0.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Hour).Substring(0, 1));
            hour_1.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Hour).Substring(1, 1));
            minute_0.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Minute).Substring(0, 1));
            minute_1.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Minute).Substring(1, 1));
            second_0.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Second).Substring(0, 1));
            second_1.SetResourceReference(Image.StyleProperty, "num_" + other.getOne(currentTime.Second).Substring(1, 1));
        }

        //任务定时器
        void eventTimer_Tick(object sender, EventArgs e)
        {
            //
        }


        // 客户端建立连接
        void ClientConn(object sender, NetEventArgs e)
        {
            //userLVConnect(e);
        }

        // 当有客户端连接时的处理
        public void OnConnectRequest(IAsyncResult ar)
        {
            while (true)
            {
                //
            }
        }

        // 客户端已满
        void ServerFull(object sender, NetEventArgs e)
        {
            string info = string.Format("Server is full. The Client:{0} is refused", e.Client.ClientSocket.RemoteEndPoint.ToString()); e.Client.Close();
        }

        /// 关闭客户端
        void ClientClose(object sender, NetEventArgs e)
        {
            string info;

            if (e.Client.TypeOfExit == Session.ExitType.ExceptionExit)
            {
                info = string.Format("A Client Session:{0} Exception Closed.",
                 e.Client.ID);
            }
            else
            {
                info = string.Format("A Client Session:{0} Normal Closed.", e.Client.ID);
            }
            if (e.Client.Channel != "")
            {
                this.m_ChannelSock.Remove(e.Client.Channel);
            }

        }

        // 接收数据
        void RecvData(object sender, NetEventArgs e)
        {
            commandCtr cmdDp = new commandCtr(e);
        }

        // 帮助信息
        private void helpBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("安徽晟峰科技有限公司--拍卖系统,详情致电联系！");
        }

        // 窗体拖拉操作
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //if (e.ChangedButton == MouseButton.Left) this.DragMove();
        }

        // 最小化
        private void minBtn_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        // 关闭窗体
        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (App.svr.IsRun)
            {
                MessageBox.Show("系统检测到服务正在开启,请退出服务后关闭程序！");
            }
            else
            {
                this.Close();
            }
        }
    }

    public class Target
    {
        public String Index { get; set; }
        public String Name { get; set; }
        public String StartTime { set; get; }
        public String EndTime { set; get; }
        public String state { set; get; }
        public bool enable { set; get; }

        public Target(String indexName, String userName, String startTime, String endTime, string state, bool isenale)
        {
            this.Index = indexName;
            this.Name = userName;
            this.StartTime = startTime;
            this.EndTime = endTime;
            this.state = state;
            this.enable = isenale;
        }

        /// <summary>
        /// 获取当前拍卖会标的列表
        /// </summary>
        /// <returns></returns>
        public static List<Target> GetSampleCustomerList()
        {
            List<Target> data = new List<Target>();
            auction auction = new auction();
            DataTable getTargetDt = new DataTable();
            getTargetDt = auction.GetTargetLst(App.currentAuctionNo);
            for (int i = 0; i < getTargetDt.Rows.Count; i++)
            {
                string type;
                string time;
                string status="";
                bool isEnable = false;

                if (getTargetDt.Rows[i]["AUCTION_TYPE"].ToString() == "AUCTION_TYPE01")
                {
                    type = "限时拍卖";
                    time = getTargetDt.Rows[i]["LIMIT_TIME"] + "分钟";
                }
                else
                {
                    type = "定时拍卖";
                    time = getTargetDt.Rows[i]["FIX_TIME"] + "秒";
                }

                switch (getTargetDt.Rows[i]["STATUS"].ToString())
                {
                    case "BID_STATUS01":
                        status = "等待";
                        isEnable = true;
                        break;
                    case "BID_STATUS02":
                        status = "已拍卖";
                        isEnable = false;
                        break;
                    case "BID_STATUS03":
                        status = "流拍";
                        isEnable = false;
                        break;
                }
                data.Add(new Target(getTargetDt.Rows[i]["AUCTION_TYPE"].ToString(), getTargetDt.Rows[i]["AUCTION_TYPE"].ToString(), type, time, status, isEnable));
            }
            return data;
        }
    }
}
